<?php
/*
 * @Author: lokei
 * @Date: 2022-09-17 16:25:29
 * @LastEditors: lokei
 * @LastEditTime: 2022-12-24 11:46:07
 * @Description: 
 */
namespace App\Http\Controllers\Platform\Type;

use App\Common\Tools\ResultTool;
use App\Http\Controllers\Controller;
use App\Models\Platform\Type\TypeModel;
use Illuminate\Http\Request;

class TypeController extends Controller {
    public function ofMode(Request $request) {
        $items = TypeModel::where('app_mode', '=', $request->input('app_mode'))->get();
        $res = ResultTool::success();
        $res['data']['items'] = $items;
        $res['data']['total'] = count($items);
        return $res;
    }

    public function ofModeRoot(Request $request) {
        $items = TypeModel::where('app_mode', '=', $request->input('app_mode'))->where('pre_id', '=', 0)->get();
        $res = ResultTool::success();
        $res['data']['items'] = $items;
        $res['data']['total'] = count($items);
        return $res;
    }

    public function ofPre(Request $request) {
        $items = TypeModel::where('pre_id', '=', $request->input('pre_id'))->get();
        $res = ResultTool::success();
        $res['data']['items'] = $items;
        $res['data']['total'] = count($items);
        return $res;
    }

    public function create(Request $request) {
        $type = new TypeModel();
        $type->app_mode = $request->input('app_mode');
        $type->name = $request->input('name');
        $type->img_thumb = $request->input('img_thumb');
		if(!is_null($request->input('pre_id')) && intval($request->input('pre_id')) > 0 ) {
			$type->pre_id = $request->input('pre_id');
			$pre_type = TypeModel::where('id', '=', $request->input('pre_id'))->first();
			$type->full_name = $pre_type->name . '|' . $request->input('name');
		}
        $type->save();
        return ResultTool::success();
    }

    public function update(Request $request) {
        $type = TypeModel::where('id', '=', $request->input('id'))->first();
        $type->name = $request->input('name');
        $type->img_thumb = $request->input('img_thumb');
		if(!is_null($request->input('pre_id')) && intval($request->input('pre_id')) > 0 ) {
			$type->pre_id = $request->input('pre_id');
			$pre_type = TypeModel::where('id', '=', $request->input('pre_id'))->first();
			$type->full_name = $pre_type->name . '|' . $request->input('name');
		}
        $type->save();
        return ResultTool::success();
    }

    public function delete(Request $request) {
        TypeModel::where('id', '=', $request->input('id'))->delete();
        return ResultTool::success();
    }
}